﻿/* Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
 
Copyright (C) 2010 by Tomasz Pluskiewicz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */

using Castle.MonoRail.Framework;

namespace Yama.MVC.MonoRail.Extensions
{
    /// <summary>
    /// Wraps <see cref="IEngineContext"/> to allow easy access to IsAjaxKey value stored in Flash
    /// </summary>
    public class AjaxContextAdapter
    {
        private static string isAjaxKey = "is.ajax";

        /// <summary>
        /// Key in Flash which stores information wheather an ajax request is being processed
        /// </summary>
        public static string IsAjaxKey
        {
            get { return isAjaxKey; }
            set { isAjaxKey = value; }
        }

        private static string isAjaxRedirectKey = "is.ajax.redirect";

        /// <summary>
        /// Key in Flash which stores information wheather an ajax request has been ordered
        /// </summary>
        public static string IsAjaxRedirectKey
        {
            get { return isAjaxRedirectKey; }
            set { isAjaxRedirectKey = value; }
        }

        /// <summary>
        /// Gets or sets the final evaluation on whether the request is ajax or not.
        /// </summary>
        public static bool IsAjax
        {
            get
            {
                Flash flash = MonoRailHttpHandlerFactory.CurrentEngineContext.Flash;

                if (flash.ContainsKey(IsAjaxKey) && flash[IsAjaxKey] is bool)
                    return (bool)flash[isAjaxKey];

                return false;
            }

            internal set { MonoRailHttpHandlerFactory.CurrentEngineContext.Flash[IsAjaxKey] = value; }
        }

        /// <summary>
        /// Gets or sets wheather a ajax redirect is being performed
        /// </summary>
        public static bool IsAjaxRedirect
        {
            get
            {
                Flash flash = MonoRailHttpHandlerFactory.CurrentEngineContext.Flash;

                if (flash.ContainsKey(IsAjaxRedirectKey) && flash[IsAjaxRedirectKey] is bool)
                    return (bool)flash[IsAjaxRedirectKey];

                return false;
            }

            internal set { MonoRailHttpHandlerFactory.CurrentEngineContext.Flash[IsAjaxRedirectKey] = value; }
        }
    }
}